package com.ccys.app.controller;

import cn.hutool.core.util.IdUtil;
import com.ccys.app.service.PlanBenefitMemberService;
import com.ccys.common.vo.ResponseResultApp;
import com.ccys.db.cache.UserUtil;
import com.ccys.db.dto.app.subscriptionAddDependant.InviteDependantSubscriptionDto;
import com.ccys.db.entity.TblPlanBenefitMember;
import com.ccys.db.mapper.TblSupplierMapper;
import com.ccys.db.vo.app.subscriptionAddDependant.FamilyInfoVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author：XLY
 * @Date：2024/1/31 0031
 */

@RestController
@RequestMapping("/subscriptionAddDependant")
@Api(tags = "Subscription Add dependant")
public class SubscriptionAddDependantController {


    @Resource
    private TblSupplierMapper mapper;

    @Resource
    private PlanBenefitMemberService benefitMemberService;

    @ApiOperation("Subscription Add dependant/1")
    @PostMapping("InviteDependantSubscription")
    @Transactional
    public ResponseResultApp inviteDependantSubscription(InviteDependantSubscriptionDto dto) {
        //profile_id 就是 benefit_id
        //删除之前的
        String profile_id = dto.getProfile_id();
        List<InviteDependantSubscriptionDto.DependantVo> dependant_ids = dto.getDependant_ids();
        if(CollectionUtils.isNotEmpty(dependant_ids)){
            mapper.deleteDependantSubscription(profile_id,dependant_ids);
            String curLoginUserId = UserUtil.getCurLoginUserId();
            FamilyInfoVo familyInfo = mapper.getFamilyInfoByMemberId(curLoginUserId, profile_id, dto.getActive_code());
            //保存
            List<TblPlanBenefitMember> collect = dependant_ids.stream().map(x -> {
                TblPlanBenefitMember benefitMember = new TblPlanBenefitMember();
                benefitMember.setId(IdUtil.getSnowflakeNextIdStr());
                benefitMember.setMemId(x.getDependant_id());
                benefitMember.setBenefitId(profile_id);
                benefitMember.setInsurerMemberId(curLoginUserId);
                benefitMember.setIdentifierInputtedByPatient(familyInfo.getIdentifierInputtedByPatient());
                benefitMember.setDependantInputtedBy(curLoginUserId);
                //这些默认值是照源码写的
                benefitMember.setActivated(1);
                benefitMember.setStatus(1);
                benefitMember.setFamilyId(familyInfo.getId());
                benefitMember.setFamilyStatus(0);
                benefitMember.setActiveCode(dto.getActive_code());
                benefitMember.setCorrelationid(familyInfo.getCorrelationid());
                return benefitMember;
            }).collect(Collectors.toList());

            benefitMemberService.insertAll(collect);

        }
        return ResponseResultApp.success();
    }
}
